package top.snoweagle.simple.console.web.controller.admin;



import  top.snoweagle.simple.console.common.enums.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import top.snoweagle.simple.console.common.domain.http.RestResponse;
import top.snoweagle.simple.console.common.domain.http.UserSession;
import top.snoweagle.simple.console.common.enums.SessionInfo;
import top.snoweagle.simple.console.security.annotation.Authorization;
import top.snoweagle.simple.console.security.auth.AuthorizingProxy;
import top.snoweagle.simple.console.web.enums.Restful;
import top.snoweagle.simple.console.security.model.AuthorizationInfo;
import top.snoweagle.simple.console.security.auth.RestfulAuthorizingProxy;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by wangshaobo on 2015/6/17.
 */
@Controller
@RequestMapping
public class LoginController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AuthorizingProxy authorizingProxy;

    @RequestMapping(value = "/")
    public String loginPage(HttpSession session) throws Exception {
        return "/admin/login";
     }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) throws Exception {
        session.invalidate();
        return "/admin/login";
    }
    @RequestMapping(value = "/doLogin",  method = RequestMethod.POST)
    public ModelAndView doLogin(HttpServletRequest request ,final RedirectAttributes attr ) throws Exception {
        String name = request.getParameter("inputName");
        String pwd = request.getParameter("inputPassword");
        //登出路径
        String respAdds = "redirect:/";


        try {
            UserSession userSession = authorizingProxy.doGetAuthentication(name, pwd);
            AuthorizationInfo authInfo = authorizingProxy.doGetAuthorization(userSession.getUserId());
            logger.warn("认证通过");
//            logger.info("菜单列表：{}", JSON.toJSON(authInfo.getAccessMenus()));
            RestResponse resp = authorizingProxy.getIndexInfo(userSession, authInfo);
            attr.addFlashAttribute(Restful.RESPONSE.getCode(), resp);
            attr.addAttribute("token", userSession.getToken());

            respAdds="redirect:/index";
            logger.info("[ 登陆成功,跳转index页面.]用户名：{}",userSession.getFullName());
         }catch (Exception e){
            logger.error(" 认证失败.[{}]", e.getMessage());
            attr.addFlashAttribute(SessionInfo.LOGIN_MESSAGE.getCode(), e.getMessage());
        }
        return new ModelAndView(respAdds);
    }



    @Authorization
    @RequestMapping(value = "/index",  method = RequestMethod.GET)
    public String redirctIndex(){

        return "/admin/index";
    }

}
